package com.fanhao.dllo.soundfm.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.fanhao.dllo.soundfm.R;
import com.fanhao.dllo.soundfm.entity.PlayListResult;
import com.fanhao.dllo.soundfm.utils.CountToOthers;

import java.util.List;

/**
 * Created by dllo on 16/1/16.
 */
public class PlayListAdapter extends BaseAdapter {
    private List<PlayListResult.TracksEntity.ListEntity> tracksList;
    private Context context;

    public PlayListAdapter(Context context) {
        this.context = context;
    }

    // 获得tracksList集合
    public void addTracksData(List<PlayListResult.TracksEntity.ListEntity> tracksList) {
        this.tracksList = tracksList;
        notifyDataSetChanged();
    }

    public List<PlayListResult.TracksEntity.ListEntity> getTracksList() {
        return tracksList;
    }

    @Override
    public int getCount() {
        return tracksList != null && tracksList.size() > 0 ?
                tracksList.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (tracksList != null && tracksList.size() > 0) {
            PlayViewHolder playHolder = null;
            if (convertView == null) {
                convertView = LayoutInflater.from(context)
                        .inflate(R.layout.play_list_adater_item, parent, false);
                playHolder = new PlayViewHolder(convertView);
                convertView.setTag(playHolder);
            } else {
                playHolder = (PlayViewHolder) convertView.getTag();
            }
            playHolder.titlePlAdapterTv.setText(tracksList.get(position).getTitle());
            playHolder.playNumPlAdapterTv.setText(CountToOthers.getCountToWan(tracksList.get(position).getPlaytimes()));
            playHolder.timePlAdapterTv.setText(CountToOthers.getCountToHour(tracksList.get(position).getDuration()));
            playHolder.commentPlAdapterTv.setText(tracksList.get(position).getComments() + "");
        }
        return convertView;
    }

    class PlayViewHolder {
        private TextView titlePlAdapterTv, playNumPlAdapterTv, timePlAdapterTv, commentPlAdapterTv;

        public PlayViewHolder(View view) {
            titlePlAdapterTv = (TextView) view.findViewById(R.id.tvTitle_playListAdapter);
            playNumPlAdapterTv = (TextView) view.findViewById(R.id.tvplayNum_playListAdapter);
            timePlAdapterTv = (TextView) view.findViewById(R.id.tvTime_playListAdapter);
            commentPlAdapterTv = (TextView) view.findViewById(R.id.tvComment_playListAdapter);
        }
    }
}
